package com.foogui.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class HelloJDKProxy implements InvocationHandler {
    private Object target;

    public HelloJDKProxy(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object result = method.invoke(target, args);
        after();
        return result;
    }

    public void before() {
        System.out.println("before");
    }
    public void after() {
        System.out.println("after");
    }

    public static void main(String[] args) {
        Hello hello = new HelloImpl();
        Hello o = (Hello)Proxy.newProxyInstance(hello.getClass().getClassLoader(),
                hello.getClass().getInterfaces(),
                new HelloJDKProxy(hello));
        o.say("我是代理类");
    }
}
